package top.jacktgq.hsp.view.groupchat;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 * 
 * @Title: ClientFrame.java 
 * @Package top.jacktgq.view.groupchat 
 * @Description: 客户端
 * @author CandyWall   
 * @date 2021年1月30日 下午4:46:43 
 * @version V1.0
 */
public class ClientFrame extends JFrame {
    public JTextArea ta;
    private Channel channel;

    public ClientFrame() {
        setLayout(new BorderLayout());
        JTextField tf = new JTextField();
        tf.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String msg = tf.getText();
                sendMsg(msg);
                tf.setText("");
            }
        });
        ta = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(ta);
        getContentPane().add(tf, BorderLayout.SOUTH);
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        initFrame();
        new Thread(() -> {
            connect();
        }).start();
    }
    
    private void sendMsg(String msg) {
        channel.writeAndFlush(msg);
    }
    
    private void initFrame() {
        setTitle("Netty客户端");
        setVisible(true);
        setSize(500, 350);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ClientFrame();
            }
        });
    }
    
    private void connect() {
        EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        
        try {
            ChannelFuture future = bootstrap.group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new StringEncoder());
                        pipeline.addLast(new StringDecoder());
                        pipeline.addLast(new ClientHandler(ta));
                    }
                })
                .connect("localhost", 8888);
            future.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if(future.isSuccess()) {
                        ta.append("登录成功！\n");
                        channel = future.channel();
                    } else {
                        ta.append("登录失败！\n");
                    }
                }
            }).sync();
            System.out.println("...");
            //channel = future.channel();
            channel.closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }
}
